package cn.nkjobsearch.jena;

import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class NKJSJena {
	public NKJSJena() {
		model = ModelFactory.createOntologyModel();
		try {
			model.read(new FileInputStream(OWL_FILE_PATH), "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	public ArrayList<int[]> findKeyword(int keywordIndex) {
		String keyword = "K" + Integer.toString(keywordIndex);
		if(resultKey.size() != 0) {
			resultKey.clear();
		}
		String relationship[] = {"equal","contain","related","belongToSameKW"};
		for(int i=0; i<relationship.length; i++) {
			String queryString = null;
			if(relationship[i].equals("belongToSameKW")) {
				queryString = QUERY_HEAD
					+ "SELECT ?keyword WHERE {" 
					+ "njs:" + keyword + " njs:containedBy ?parent." +
							"?parent njs:contain ?keyword "
					+ "}";
 			} else {
 				queryString = QUERY_HEAD
					+ "SELECT ?keyword WHERE {" 
					+ "njs:" + keyword + " njs:" + relationship[i] + " ?keyword "
					+ "}";
 			}
			QueryExecution qe = QueryExecutionFactory.create(queryString, model);
			ResultSet rs = qe.execSelect();
			boolean equalToBelongToSameKW = relationship[i].equals("belongToSameKW");
			while (rs.hasNext()) {
				String temp = rs.next().toString();
				int begin = temp.indexOf("#");
				int end = temp.indexOf(">");
				int contents[] = new int[]{Integer.parseInt(temp.substring(begin + 2, end)), i};
				if(!equalToBelongToSameKW || (!alreadyExist(resultKey, contents) && contents[0] != keywordIndex ) ) {
					resultKey.add(contents);
				}
			}
			qe.close();
		}
		return resultKey;
	}

	private boolean alreadyExist(ArrayList<int[]> result, int[] obj) {
		for(int i=0; i<result.size(); i++) {
			if(result.get(i)[0] == (obj[0])) {
				return true;
			}
		}
		return false;
	}
	/**
	 * findType: false-寻找周边的城市 true-同省下的城市
	 * */
	public ArrayList<Integer> findCity(int cityIndex, boolean findType) {
		String city = "C" + Integer.toString(cityIndex);
		if(result.size() != 0) {
			result.clear();
		}
		String queryString;
		if(findType == false){	//nearby city
			queryString = QUERY_HEAD + "SELECT ?x WHERE { " + "?x njs:nearCity njs:" + city + " }";
		} else {	// belong to the same province
			queryString = QUERY_HEAD + "SELECT ?x WHERE { " + "njs:" + city + " njs:belongToProv ?prov . " +
					"?prov njs:hasCity ?x }";	
		}
		QueryExecution qe = QueryExecutionFactory.create(queryString, model);
		ResultSet rs = qe.execSelect();
		while (rs.hasNext()) {
			String temp = rs.next().toString();
			int begin = temp.indexOf("#");
			int end = temp.indexOf(">");
			Integer contents = Integer.parseInt(temp.substring(begin + 2, end));
			//在查找同省市的时候，去掉和city相同的值
			if( !findType || !contents.equals(city) ) {
				result.add(contents);
			}
		}
		qe.close();
		return result;
	}

	public static void useSapmle(String[] args) {
		System.out.println("Sstart running ...");
		NKJSJena jn = new NKJSJena();
		int keyword = 1;
		ArrayList<int[]> rs = jn.findKeyword(keyword);
		for (int i = 0; i < rs.size(); i++) {
			int o[] = rs.get(i);
			System.out.println(o[0] + "\t" + o[1]);
		}
		int city = 10;
		ArrayList<Integer> rs2 = jn.findCity(city, true);
		for (int i = 0; i < rs2.size(); i++) {
			System.out.println(rs2.get(i));
		}
		System.out.println("Exit!");
	}
	
	private static final String OWL_FILE_PATH = "E:\\Study\\Search\\googleCode\\trunk\\owl\\nkjs.cn.owl";
	private final String QUERY_HEAD = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
		+ "PREFIX njs: <http://nkjs.cn/nkjs.owl#> "; 
	private ArrayList<Integer> result = new ArrayList<Integer>();
	private ArrayList<int[]> resultKey = new ArrayList<int[]>();
	private OntModel model = null;

}
